<?php


namespace app\yunda\home;
use app\common\controller\Common;
use EasyWeChat\Foundation\Application;
use think\Controller;
use think\Request;
use app\yunda\model\Postage;
use lib\Pay;
/**
 * 仪表盘控制器
 * @package app\cms\admin
 */
class Base extends Common {

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function test()
    {
        $config=config('weiXin');
        $appid=$config['appid'];
        $secret=$config['secret'];
        $options = [
            'debug'  => true,
            'app_id' =>$appid,
            'secret' => $secret,
            'token'  => 'easywechat',

            // 'aes_key' => null, // 可选

            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ],

            //...
        ];

        $app = new Application($options);

        $response = $app->oauth->scopes(['snsapi_userinfo'])
            ->redirect();
        $user = $app->oauth->user();
        halt($user);
    }

    public function miss()
    {
        return '无效的路由-_-';
    }

    public function get_user_info()
    {
        $code=request()->only(['code']);
        //code获取openid
        $config=config('weiXin');
        $appid=$config['appid'];
        $secret=$config['secret'];
        //登录凭证校验接口
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid.'&secret='.$secret."&js_code=$code&grant_type=authorization_code";
        //code获取用户openid和session_key
        $info = json_decode(get_curl($url), true);
        if(!isset($info['session_key'])) return mogo_error($info,401);
        $openid=$info['openid'];
        //获取access_token
//        https请求方式: GET
//        https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        $url1="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $access_token = json_decode(get_curl($url1), true)['access_token'];
        //3获取用户信息
//        http请求方式: GET
//        https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        $url_2="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
        $user_info = json_decode(get_curl($url_2), true);
        halt($user_info);

    }

}